/**
 * @file    drv_i2c_soft.h
 * @brief   底层驱动-i2c模拟驱动
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-04-22
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-22 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */

#ifndef __DRV_I2C_SOFT_H_
#define __DRV_I2C_SOFT_H_

/* file includes -------------------------------------------------------------*/
#include "drv_gpio.h"
#include "drv_gpio_pcfg.h"
#include "drv_mcu.h"
/* cplusplus -----------------------------------------------------------------*/
#ifdef __cplusplus
extern "C" {
#endif

/* macro/define/typedef ------------------------------------------------------*/
/**
 * @brief I2C 接口类型定义
 */
typedef struct 
{
	GPIO_PORT scl_port;
	GPIO_PIN scl_pin ;
	GPIO_PORT sda_port;
	GPIO_PIN sda_pin ;
}i2c_port_type;

enum
{
	DRV_I2C0 ,
	DRV_I2C1 ,
	DRV_I2C_MAX ,
};

/* @brief */

/**
 * @brief I2C模块 GPIO接口
 */
#define i2c_cfg_scl_out(i2c_obj)    drv_gpio_cfg_mode(i2c_obj.scl_port , i2c_obj.scl_pin , GPIO_MODE_OUTPUT)
#define i2c_clr_scl(i2c_obj)        drv_gpio_reset_pin(i2c_obj.scl_port , i2c_obj.scl_pin)
#define i2c_set_scl(i2c_obj)        drv_gpio_set_pin(i2c_obj.scl_port , i2c_obj.scl_pin)

#define i2c_cfg_sda_out(i2c_obj)    drv_gpio_cfg_mode(i2c_obj.sda_port , i2c_obj.sda_pin , GPIO_MODE_OUTPUT)
#define i2c_cfg_sda_in(i2c_obj)     drv_gpio_cfg_mode(i2c_obj.sda_port , i2c_obj.sda_pin , GPIO_MODE_INPUT)
#define i2c_clr_sda(i2c_obj)        drv_gpio_reset_pin(i2c_obj.sda_port , i2c_obj.sda_pin)
#define i2c_set_sda(i2c_obj)        drv_gpio_set_pin(i2c_obj.sda_port , i2c_obj.sda_pin)
#define i2c_read_sda(i2c_obj)       drv_gpio_read_pin(i2c_obj.sda_port , i2c_obj.sda_pin)

#define i2c_delay_us(us)            mcu_delay_us(us)  

#define DEV_I2C_WR                  (0)  /* !< 写指令 */
#define DEV_I2C_RD                  (1)  /* !< 读指令 */
/* variables -----------------------------------------------------------------*/

/*******************************************************************************
 * public API List
 ******************************************************************************/
void drv_i2c_soft_init(uint8_t i2c_id);
void drv_i2c_release(uint8_t i2c_id);
void drv_i2c_soft_start(uint8_t i2c_id);
void drv_i2c_soft_stop(uint8_t i2c_id);
void drv_i2c_soft_write_ack(uint8_t i2c_id);
void drv_i2c_soft_write_nack(uint8_t i2c_id);
uint8_t drv_i2c_soft_read_ack(uint8_t i2c_id);
uint8_t drv_i2c_soft_read_byte(uint8_t i2c_id);
void drv_i2c_soft_write_byte(uint8_t i2c_id ,uint8_t byte);
uint8_t drv_i2c_soft_check_device(uint8_t i2c_id, uint8_t dev_addr);

#ifdef __cplusplus
}
#endif

#endif  /* #ifndef __DRV_I2C_H_ */
/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/
